package com.common.util;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通用的网络请求
 * <p>
 * Created by wgf on 16/11/11.
 *
 * @author wgf
 * @since v4.2.0
 */

public class CommonHttpBiz {

    public static Map<String, Object> handleEntity(JSONObject response, boolean isShowToast) {
        if (OrangeErrorHandler.getInstance().isSuccess(response, isShowToast)) {
            return OrangeDataHandler.getMapWithEntityNoGson(response);
        }
        return null;
    }

    public static Map<String, Object> handleEntity(JSONObject response) {
        return handleEntity(response, true);
    }

    public static Map<String, Object> handleEntityWithoutToast(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response, false)) {
            return OrangeDataHandler.getMapWithEntityNoGson(response);
        }
        return null;
    }

    //new TypeToken<List<WashCarChooseEntity>>() {}.getType()
    public static List handlePageEntity(JSONObject response, Type type) {
        List list = new ArrayList<>();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, type, null, null);
        }
        return list;
    }

    public static List<Map<String, Object>> handleListMap(JSONObject response) {
        List<Map<String, Object>> list = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);
        }
        return list;
    }

}
